/*Storage*/

// 刷新时间 30 分钟
const refreshTime = 1000 * 60 * 60 * 30;

/**
 * 保存
 * @param key
 * @param object 需要保存的源数据
 * @param isLocal 是否local级别
 */
export function saveStorage(key, object, isLocal){
  let sto = sessionStorage;
  if (isLocal) {
    sto = localStorage;
  }
  let obj = {__data: object, __time: new Date().getTime()};
  sto.setItem(key, JSON.stringify(obj));
}

/**
 * 获取
 * @param key
 * @param isLocal 是否local级别
 * @returns {null|*}
 */
export function  getStorage(key, isLocal){
  let sto = sessionStorage;
  if (isLocal) {
    sto = localStorage;
  }
  let time = new Date().getTime();
  let obj_str = sto.getItem(key);
  if (!obj_str) {
    return null;
  }
  let obj = JSON.parse(obj_str);
  let {__time} = obj;
  if (time - __time > refreshTime) {
    sto.removeItem(key);
    return null;
  }
  return obj.__data;
}

/**
 * 保存 - 没有周期限制
 * @param key
 */
export function saveStorageNoTime(key, object){
  localStorage.setItem(key, JSON.stringify(object));
}

/**
 * 获取 - 没有周期限制
 * @param key
 */
export function getStorageNoTime(key){
  let obj_str = localStorage.getItem(key);
  return !obj_str ? null : JSON.parse(obj_str);
}

/**
 * 清理
 * @param key
 * @param isLocal
 */
export function clearStorage(key, isLocal){
  let sto = sessionStorage;
  if (isLocal) {
    sto = localStorage;
  }
  sto.removeItem(key);
}
